#lang scheme

(require "stream-basics.rkt")

(define plus-count 0)

(define (plus x y)
  (set! plus-count (+ plus-count 1))
  (+ x y))

(define (add-streams s1 s2)
  (stream-map plus s1 s2))

(define fibs
  (cons-stream 0
               (cons-stream 1
                            (add-streams (stream-cdr fibs)
                                         fibs))))

(display "plus-count: ")
(display plus-count)
(display "\n")

(display-stream (stream-take 10 fibs))

(display "plus-count: ")
(display plus-count)
(display "\n")

(define (lambda-hull l1 l2)
  (lambda () (let ((result (plus (l1) (l2))))
               (display result)
               (display " ")
               result)))

(define (plus-streams s1 s2)
  (stream-map lambda-hull s1 s2))

(define lambda-fibs
  (cons-stream (lambda () (display 0) (display "\n") 0)
               (cons-stream (lambda () (display 1) (display "\n") 1)
                            (plus-streams (stream-cdr lambda-fibs)
                                          lambda-fibs))))

(set! plus-count 0)

(display "plus-count: ")
(display plus-count)
(display "\n")

((stream-ref lambda-fibs 9))

(display "plus-count: ")
(display plus-count)
(display "\n")
